/**
 * 生成分区树
 * @param data
 * @returns {Array}
 */
function toTree(data){
    var arr=[];
    var arr2=[];
    angular.forEach(data,function(item){
        if(arr.length!=0){
            angular.forEach(arr,function(k,index){
                if(item.fname==k.fname){
                    var obj={fname:item.fname,partition:[item]};
                    k.partition.push(item);
                }else{
                    if(index==arr.length-1){
                        var obj={fname:item.fname,partition:[item]};
                        arr.push(obj);
                    }
                }
            });
        }else{
            var obj={fname:item.fname,partition:[item]};
            arr.push(obj);
        }

    });
    return arr;
}
/**
 * 获取当天晚上十一点
 * @returns {Date}
 */
function getToday11Clock(){
    var date=new Date();
    date.setHours(23);
    date.setMinutes(59);
    date.setSeconds(59);
    date.setMilliseconds(999);
    return date;
}
/**
 * 获取当天零点
 * @returns {Date}
 */
function getToday00Clock(){
    var date=new Date();
    date.setHours(0);
    date.setMinutes(0);
    date.setSeconds(0);
    date.setMilliseconds(0);
    return date;
}

//获得本周的开始日期
function getWeekStartDate() {
    var now = new Date(); //当前日期
    var nowDayOfWeek = now.getDay();
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    console.log(nowDayOfWeek);
    console.log(nowDay);
    var weekStartDate = new Date(nowYear, nowMonth, (nowDay - nowDayOfWeek)+1);
    return weekStartDate;
}
//获得本周的结束日期
function getWeekEndDate() {
    var now = new Date(); //当前日期
    var nowDayOfWeek = now.getDay();
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek));
    return weekEndDate;
}

//获得上周的开始日期
function getLastWeekStartDate() {
    var now = new Date(); //当前日期
    var nowDayOfWeek = now.getDay();
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6);
    return weekStartDate;
}
//获得上周的结束日期
function getLastWeekEndDate() {
    var now = new Date(); //当前日期
    var nowDayOfWeek = now.getDay();
    var nowDay = now.getDate(); //当前日
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    var weekEndDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek );
    return weekEndDate;
}
//获得本月的开始日期
function getMonthStartDate() {
    var now = new Date(); //当前日期
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    var monthStartDate = new Date(nowYear, nowMonth, 1);
    return monthStartDate;
}
//获得本月的结束日期
function getMonthEndDate() {
    var now = new Date(); //当前日期
    var nowMonth = now.getMonth(); //当前月
    var nowYear = now.getFullYear(); //当前年
    var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    return monthEndDate;
}
//获得上月开始时间
function getLastMonthStartDate() {
    var now = new Date(); //当前日期
    var lastMonthDate = new Date(); //上月日期
    lastMonthDate.setDate(1);
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
    var lastMonth = lastMonthDate.getMonth();
    var nowYear = now.getFullYear(); //当前年
    if (lastMonth+1 == 12){
        nowYear =now.getFullYear()-1;
    }
    var lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return lastMonthStartDate;
}
//获得上月结束时间
function getLastMonthEndDate() {
    var now = new Date(); //当前日期
    var nowYear = now.getFullYear(); //当前年

    var lastMonthDate = new Date(); //上月日期
    lastMonthDate.setDate(1);
    lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
    var lastMonth = lastMonthDate.getMonth();
    if (lastMonth+1 == 12){
        nowYear =now.getFullYear()-1;
    }
    var lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return lastMonthEndDate;
}
//获得某月的天数
function getMonthDays(myMonth) {
    var now = new Date(); //当前日期
    var nowYear = now.getFullYear(); //当前年
    var monthStartDate = new Date(nowYear, myMonth, 1);
    var monthEndDate = new Date(nowYear, myMonth + 1, 1);
    var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
}

Date.prototype.Format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
};

/**
 * 生成下载／打开原文的地址
 * @param dataItem
 * @param ip
 * @returns {string}
 */
function generrateDownAddress(dataItem,ip){
    //日期处理，相差多少天
    var time1 = Date.parse(dataItem.logtime.split(" ")[0]) / 1000;
    var time2 = Date.parse(new Date().Format("yyyy-MM-dd")) / 1000;
    var time = time2 - time1;
    time = time / (3600 * 24);

    var address = "http://" + ip + ":8001/Trace/" + dataItem.fileName;
    if (time != 0) {
        address = "http://" + ip + ":8001/Trace." + time + "/" + dataItem.fileName;
    }
    return address;
}

/**
 * 判断浏览器类型
 * @constructor
 */
function browserType() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
    var isEdge = userAgent.indexOf("Windows NT 6.1; Trident/7.0;") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isFF = userAgent.indexOf("Firefox") > -1; //判断是否Firefox浏览器
    var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1; //判断是否Safari浏览器
    var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1; //判断Chrome浏览器

    if(isOpera){return 'Opera';}
    if(isIE){return 'IE';}
    if(isEdge){return 'Edge';}
    if(isFF){return 'FF';}
    if(isSafari){return 'Safari';}
    if(isChrome){return 'Chrome';}
}

/**
 * 改变日期格式
 * @param num
 * @returns {*}
 */
function getDate(str) {
    if (str == null || str == "") {
        return "";
    }
    var oDate = new Date(str),
        oYear = oDate.getFullYear(),
        oMonth = oDate.getMonth() + 1,
        oDay = oDate.getDate(),
        oHour = oDate.getHours(),
        oMin = oDate.getMinutes(),
        oSen = oDate.getSeconds(),
        oMill = oDate.getMilliseconds();
    var oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen) + "." + getzf2(oMill);//最后拼接时间
    return oTime;
}

/**
 * 计算两个时间相差多少秒
 * @param startDate
 * @param endDate
 * @returns {number}
 */
function getDiffSeconds(startDate, endDate) {
    var ms = endDate.getTime() - startDate.getTime();
    if (ms < 0) return 0;
    return Math.floor(ms/1000);
}


function getzf(num) {
    if (parseInt(num) < 10) {
        num = '0' + num;
    }
    return num;
}

function getzf2(num) {
    if (parseInt(num) < 10) {
        num = '00' + num;
    } else if (parseInt(num) < 100) {
        num = '0' + num;
    }
    return num;
}
function timestampToTime(time) {
    var date = new Date(time);
    var h = date.getHours();
    var mm = date.getMinutes();
    return add0(h) + ':' + add0(mm);
}
function changeMin(time) {
    return time / 1000 / 60
}
function add0(m) {
    return m < 10 ? '0' + m : m
}
